package com.erWeiMa;

/**
 * 生成二维码
 *
 * @author zhanghuibo
 * @version 1.0.0
 * @package com.erWeiMa
 * @className QrCodeTest
 * @date 2025-03-31  14:25
 */

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import org.junit.jupiter.api.Test;
import org.springframework.util.ResourceUtils;
import com.google.zxing.EncodeHintType;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;

public class QrCodeTest {


    //直接根据url生成二维码
//    @Test
//    void test1() {
//        QrCodeUtil.generate("https://www.cnblogs.com/studyjobs",
//                300, 300, FileUtil.file("D:\\qrcode1.jpg"));
//    }

    //设置二维码前景色、背景色、中间的logo
    @Test
    void test2() throws FileNotFoundException {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(1);
        // 设置前景色，二维码颜色（绿色）
//        config.setForeColor(new Color(0, 139, 69));
//        // 设置背景色（淡黄色）
//        config.setBackColor(new Color(255, 255, 224));
        // 设置中间的 logo 图片
        String logoPath = ResourceUtils.getFile("classpath:logo/logo.png").getPath();
        config.setImg(logoPath);
        // 设置容错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        // 生成二维码到文件，也可以到流
        QrCodeUtil.generate("https://scf.app.com.cn:10442/poster/poster.html",
                config, FileUtil.file("/Users/zhanghuibo/Downloads/qrcode2.png"));
    }

//    //生成二维码的base64字符串
//    @Test
//    void test3() {
//        byte[] pngArr = QrCodeUtil.generatePng("https://www.cnblogs.com/studyjobs",
//                300, 300);
//        String base64 = Base64Utils.encodeToString(pngArr);
//        System.out.println(base64);
//
//        //将 base64 字符串，放入 html 的 image 标签中，打开 html 就可以看到二维码图片
//        //格式为：<img src="data:image/png;base64,你的图片的base64写在这里"/>
//        //我们可以让 http 接口返回图片的 base64 字符串到前端，相当于提供了二维码图片下载接口
//    }
//
//    //生成二维码的base64字符串
//    @Test
//    void test4() throws FileNotFoundException {
//        QrConfig config = new QrConfig(300, 300);
//        // 设置边距，既二维码和背景之间的边距
//        config.setMargin(1);
//        // 设置前景色，二维码颜色（绿色）
//        config.setForeColor(new Color(0, 139, 69));
//        // 设置背景色（淡黄色）
//        config.setBackColor(new Color(255, 255, 224));
//        // 设置中间的 logo 图片
//        String logoPath = ResourceUtils.getFile("classpath:logo/logo2.png").getPath();
//        config.setImg(logoPath);
//        // 设置容错级别
//        config.setErrorCorrection(ErrorCorrectionLevel.H);
//        byte[] pngArr = QrCodeUtil.generatePng("https://www.cnblogs.com/studyjobs", config);
//        String base64 = Base64Utils.encodeToString(pngArr);
//        System.out.println(base64);
//
//        //将 base64 字符串，放入 html 的 image 标签中，打开 html 就可以看到二维码图片
//        //格式为：<img src="data:image/png;base64,你的图片的base64写在这里"/>
//        //我们可以让 http 接口返回图片的 base64 字符串到前端，相当于提供了二维码图片下载接口
//    }
//
//    //识别二维码中的 url 信息
//    @Test
//    void test5() throws FileNotFoundException {
//        File qrFile = ResourceUtils.getFile("classpath:img/QrCode.png");
//        String decode = QrCodeUtil.decode(qrFile);
//        System.out.println(decode);
//    }
//
//    //识别 base64 图片中的 url 信息
//    @Test
//    void test6() {
//        String base64 = "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAIAAAD2HxkiAAAR/0lEQVR42u3de1BU1x3A8fzRmFi1SXxMYtR0Wp2xTsyjOlaT6PiKBqcqSSaxraYZ83DUOk3tpD5RooJPYlQUfCGI4gsERZCHCOyyuDx3FYRFEIkYRa1EQ1SMAtqfe3W7hGcSde+9fH/zm3VdLpd93M/+ztlzz9nH7tw5TZKkC/MxngKSBCFJgpAkSRCSJAhJkgQhSYKQJEkQkiQISZIEIUmCkCRJEJIkCEmSBCFJgpAkSRCSJAhJklQlwsd8RmgiH9T9f9jPj1ae54d9P/V6vIEQhCAEIQhBCEIQghCEIAQhCEEIQhCCEIQgBCEIQQhCEOodocuGOz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YH60EIQhCCEIQgBCEIQQhCEIIQhCAEIQhBCEIQghCEIAQhCEGomUnJajvIXLV4rlb+LghBCEIQghCEIAQhCEEIQhCCEIQgBCEIQQhCEIIQhCAEIQi1OVivlcmjWn9+9IochCAEIQhBCEIQghCEIAQhCEEIQhCCEIQgBCEIQQhCEIIQhNpcxFbrJwmo7fnXyptUi5jUC0IQghCEIAQhCEEIQhCCEIQgBCEIQQhCEIIQhCAEIQhB+BAR6nWSq6sOJle9Cbrq5Aq1HQ+anNQLQhCCEIQgBCEIQQhCEIIQhCAEIQhBCEIQghCEIAQhCEH4EBGSJAlCkgQhSZIgJEkQkiQJQpIEIUmSICRJEJIkCUKSBCFJkiAkSRCSJAlCkgQhSZIgJEkQkiQJQpLUSP4P+J3wjePIbYwAAAAASUVORK5CYII=";
//        byte[] bytes = Base64Utils.decodeFromString(base64);
//        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
//        String decode = QrCodeUtil.decode(bis);
//        System.out.println(decode);
//    }
}
